package astrologer.validator;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author hungnp
 */
@ManagedBean
@RequestScoped
public class MobileValidator implements Validator{

    /** Creates a new instance of MobileValidator */
    public MobileValidator() {
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String enteredMobile = (String) value;
        Pattern p = Pattern.compile("\\d{10,11}");
        Matcher m = p.matcher(enteredMobile);
        boolean matchFound = m.matches();

        if (!matchFound) {
            FacesMessage message = new FacesMessage();
            message.setSummary("Invalid Mobile ID.");
            throw new ValidatorException(message);
        }

    }
}
